import json

import spider
import analysis


def main():
    print('Welcome to Manage Tool\n')
    print('1 爬虫')
    print('2 数据处理')
    print('0 退出')
    num_input = input('Input> ')
    if num_input.isdecimal():
        num_input = int(num_input)
        if num_input == 0:
            exit()
        elif num_input == 1:
            spider_menu()
        elif num_input == 2:
            analysis_menu()
        else:
            print('Wrong Number!')
    else:
        raise ValueError()


def spider_menu():
    print('')
    print('1 京东万象爬虫')
    print('2 阿里云云市场爬虫')
    print('3 腾讯云市场爬虫')
    print('0 退出')
    num_input = input('Input> ')
    if num_input.isdecimal():
        num_input = int(num_input)
        if num_input == 0:
            exit()
        elif num_input == 1:
            new_spider_obj = spider.SpiderJdwx()
            run_spider(new_spider_obj, 'jdwx')
        elif num_input == 2:
            new_spider_obj = spider.SpiderAliyun()
            run_spider(new_spider_obj, 'aliyun')
        elif num_input == 3:
            new_spider_obj = spider.SpiderTencent()
            run_spider(new_spider_obj, 'tencent')
        else:
            print('Wrong Number!')
    else:
        raise ValueError()


def analysis_menu():
    print('')
    print('1 生成CSV文件')
    print('2 生成京东万象数据报告')
    print('2 生成腾讯云市场数据报告')
    print('0 退出')
    num_input = input('Input> ')
    if num_input.isdecimal():
        num_input = int(num_input)
        if num_input == 0:
            exit()
        elif num_input == 1:
            analysis.to_csv('data/jdwx.csv', analysis.jdwx_price_process('data/jdwx_app_info.json'), 'jdwx')
            analysis.to_csv('data/tencent.csv', analysis.tencent_price_process('data/tencent_app_info.json'), 'tencent')
            print('Done!')
        elif num_input == 2:
            analysis.get_analysis('data/jdwx.csv', 'data/jdwx_report.html')
            print('Done!')
        elif num_input == 3:
            analysis.get_analysis('data/tencent.csv', 'data/tencent_report.html')
            print('Done!')
        else:
            print('Wrong Number!')
    else:
        raise ValueError()


def run_web():
    from flask import Flask
    app = Flask(__name__, static_folder='web')

    @app.route('/')
    def index():
        return app.send_static_file('index.html')

    @app.route('/<path:path>')
    def return_file(path):
        return app.send_static_file(path)

    app.run()


def run_spider(spider_obj, name):
    def _run_spider(_app_list):
        app_info, error_list = spider_obj.run(_app_list, 64)
        with open('data/' + name + '_app_info.json', 'w') as fp:
            json.dump(app_info, fp, ensure_ascii=False, indent=4)
        with open('data/' + name + '_error_list.json', 'w') as fp:
            json.dump(error_list, fp, ensure_ascii=False, indent=4)
        print('Done! With {0} errors.'.format(len(error_list)))

    print()
    print('1 一键开始')
    print('2 获取列表信息并储存')
    print('3 获取全部商品信息并储存')
    print('4 获取指定商品信息并输出')
    print('0 退出')
    num_input = input('Input> ')
    if num_input.isdecimal():
        num_input = int(num_input)
        if num_input == 0:
            exit()
        elif num_input == 1:
            app_list = spider_obj.run_get_app_list()
            with open('data/' + name + '_app_list.json', 'w') as f:
                json.dump(app_list, f, ensure_ascii=False, indent=4)
            _run_spider(app_list)
        elif num_input == 2:
            app_list = spider_obj.run_get_app_list()
            with open('data/' + name + '_app_list.json', 'w') as f:
                json.dump(app_list, f, ensure_ascii=False, indent=4)
        elif num_input == 3:
            with open('data/' + name + '_app_list.json', 'r') as f:
                app_list = json.load(f)
            _run_spider(app_list)
        elif num_input == 4:
            print(json.dumps(spider_obj.get_app_info(input('请输入合法的网址: ')), ensure_ascii=False, indent=4))
        else:
            print('Wrong Number!')
    else:
        raise ValueError()


if __name__ == '__main__':
    main()
